/*
 * OpenDialog.java
 * @author Andrey Tesler
 * Created for DiffView project
 * Created on 4 October 2009, 13:55
 */

package diffview.gui;

import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import static java.lang.System.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import static javax.swing.BoxLayout.*;
import static javax.swing.Box.*;

/**
 * A dialog to choose an old version of a file and a new one. 
 */
public class OpenDialog extends JDialog {
    
    //From DirectoryChooser (from the last year's project)
    
    final JTextField openDirField = new JTextField(25);
    final JTextField saveDirField = new JTextField(25);
    final JTextField[] textFields = new JTextField[] {openDirField, saveDirField};
    JButton okButton, cancelButton;
    private String[] myResults = new String[2];
    
    
    public OpenDialog(JFrame parent) {
        super(parent, "Choose files to open");
        setLocation(100, 100);
        setResizable(false);
        //setName("Choose files to open");
        setModal(true);

        okButton = new JButton("OK"); 
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                myResults[0] = openDirField.getText();
                myResults[1] = saveDirField.getText();
                setVisible(false);
            }
        });
        cancelButton = new JButton("Cancel"); 
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent ev) {
                myResults[0] = null;
                myResults[1] = null;
                setVisible(false);
            }
        });
        
        arrangeComponents();
    }
    
    void arrangeComponents() {
        Box box = new Box(BoxLayout.Y_AXIS);
        box.setBorder(BorderFactory.createEmptyBorder(12,12,12,12));

        /* Add 2 similar parts */
        for (int i = 0; i < 2; i++) {
            Box labelBox = new Box(X_AXIS);
                labelBox.add(new JLabel
                        (i == 0 ? "Choose the old version:"
                                : "Choose the new version:"));
                labelBox.add(createGlue());
            box.add(labelBox);
            box.add(createVerticalStrut(5));
            Box fieldBox = new Box(X_AXIS); 
                fieldBox.add(textFields[i]);
                fieldBox.add(createHorizontalStrut(5));
                fieldBox.add(new JButton(new BrowseAction(i)));
            box.add(fieldBox);
            box.add(createVerticalStrut((i == 0) ? 12 : 18));
        }

        JPanel flowPanel = new JPanel(new FlowLayout
                            (FlowLayout.RIGHT, /*hgap = */ 5, /*vgap = */ 0));
            JPanel gridPanel = new JPanel
                               (new GridLayout(/*rows = */ 1, /*cols = */ 2, 
                                               /*hgap = */ 5, /*vgap = */ 0));
                gridPanel.add(okButton);
                gridPanel.add(cancelButton);
            flowPanel.add(gridPanel);
        box.add(flowPanel);
        
        add(box);
        pack();
    }
    
    
    String[] getResults() {
        return myResults;
    }
    
    
    
    class BrowseAction extends AbstractAction {
        final int i;
        
        BrowseAction(int number) {
            i = number;
            putValue(Action.NAME, "Browse");
        }
        
        public void actionPerformed(ActionEvent ev) {
            JFileChooser chooser = new JFileChooser(/*default directory*/);
            //chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
            chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter(){
                public boolean accept(java.io.File file) {
                    return true;
                }
                public String getDescription() {
                    return "All!!!";
                }
            });
            
            if (chooser.showDialog(OpenDialog.this, "Choose the file")
                    == JFileChooser.APPROVE_OPTION) {
                textFields[i].setText(chooser.getSelectedFile().getPath());
            }
            ///TODO process errors?

        }
    }

}
